package com.lfy.modules.common.listener;


import com.lfy.modules.common.custom.ApplicationStart;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
/**
 *  启动任务
 *
 * @author <a href="https://www.lvfeiy.top">lvfeiyang</a>
 * @date 2024/4/25 0:58
 */
@Component
@Slf4j
public class RefreshEventListener {
    @Resource
    private ThreadPoolExecutor mainThreadPool;
    @EventListener(classes = {ContextRefreshedEvent.class})
    public void contextRefreshedEventListener(ContextRefreshedEvent event){
        ApplicationContext applicationContext = event.getApplicationContext();
        if(Objects.isNull(applicationContext.getParent())){

            Map<String, ApplicationStart> beans = applicationContext.getBeansOfType(ApplicationStart.class);
            CompletableFuture.runAsync(()->{
                beans.forEach((k,v)->{
                    log.info("{} init",k);
                    v.init();
                });
            },mainThreadPool);





        }
    }
}
